package com.xwb.service.impl;

import com.xwb.cmp.Mycomparator;
import com.xwb.entity.*;
import com.xwb.mapper.*;
import com.xwb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private WarehouseMapper warehouseMapper;
    @Autowired
    private InstorageMapper instorageMapper;
    @Autowired
    private OutstorageMapper outstorageMapper;
    @Autowired
    private ProviderMapper providerMapper;
    @Autowired
    private RetailerMapper retailerMapper;

    @Override
    public String register(String username, String psd, String qpsd,String phone) {
        List<User> users = userMapper.selectList(null);
        User user = users.stream().filter(x -> x.getUsername().equals(username)).findAny().orElse(new User(0, null, null, "", "", "", 0));

        if(username.equals("")) return "用户名为空";
        else if(user.getUsername().equals(username)) return "用户名已被使用";
        else if(!psd.equals(qpsd)) return "确认密码与密码不一致";
        else if(phone.equals("")) return "手机号为空";
        else{
            int insert = userMapper.insert(new User(username,psd, phone, 0));//密码注册之前要加密
            System.out.println(insert+"======================");
            if(insert==1)  return "注册成功";
            else return "写入数据库失败";
        }
    }

    @Override
    public List<Warehouse> userQureyAllWarHouse() {
        List<Warehouse> warehouses = warehouseMapper.selectList(null);
        return warehouses;
    }

    @Override
    public List<Order> userQueryAllInstorage() {
        List<Order> hashMaps = userMapper.userQueryAllInstorage();
        //hashMaps.forEach(System.out::println);
        return hashMaps;
    }

//    @Override
//    public List<HashMap<String,Object>> userQueryAllInstorage() {
//        List<Instorage> instorages = instorageMapper.selectList(null);
//        ArrayList<HashMap<String,Object>> hashMaps = new ArrayList<>();
//        for (Instorage instorage : instorages) {
//            HashMap<String, Object> Map = new HashMap<>();
//            Map.put("id",instorage.getInstorageid());
//            Map.put("productId",instorage.getProductid());
//            Map.put("productName",instorage.getProductname());
//            Map.put("price",instorage.getPrice());
//            Map.put("instorageTime",instorage.getInstoragetime());
//            Map.put("number",instorage.getNumber());
//            Map.put("providerName",providerMapper.selectById(instorage.getProviderid()).getProvidername());
//            Map.put("operatorName",userMapper.selectById(instorage.getOperatorid()).getUsername());
//            hashMaps.add(Map);
//        }
//        return hashMaps;
//    }

    @Override
    public List<Warehouse> userQureyAllWarHouseBySort() {
        List<Warehouse> warehouses = warehouseMapper.selectList(null);
        Collections.sort(warehouses, new Mycomparator());
        return warehouses;
    }

    @Override
    public List<Map<String,Object>> userQueryAllOutstorage() {
        List<Map<String, Object>> maps = outstorageMapper.selectMaps(null);
        for (Map<String, Object> map : maps) {
            map.put("providerName",providerMapper.selectById((int)map.get("retailerId")).getProvidername());
            map.put("operatorName",userMapper.selectById((int)map.get("operatorId")).getUsername());
        }
        return maps;
    }

    @Override
    public String inOldStorage(Integer productId,Integer number,Integer operatorId) {
        Warehouse warehouse = warehouseMapper.selectById(productId);//找到要插入的货物对应的库存货物记录
        if(warehouse==null) return "此商品为新商品，请选择进新货";
        else {
            Instorage instorage = new Instorage(productId, warehouse.getProductname(), warehouse.getPrice(), new Date(), number, warehouse.getProviderid(), operatorId);
            int insert = instorageMapper.insert(instorage);
            warehouse.setNownumber(warehouse.getNownumber()+number);
            warehouseMapper.updateById(warehouse);
            return "货物加入成功";
        }
    }

    @Override
    public String inNewStorage(Instorage instorage) {
        int insert = instorageMapper.insert(instorage);
        Warehouse warehouse = warehouseMapper.selectById(instorage.getProductid());//找到要插入的货物对应的库存货物记录
        Provider provider = providerMapper.selectById(instorage.getProviderid());
        if(warehouse!=null) return "商品号已存在,请选择进货";
        else if(provider==null) return "此供应商不存在";
        else {
            int insert1 = warehouseMapper.insert(new Warehouse(instorage.getProductid(), instorage.getProductname(), instorage.getPrice(), instorage.getNumber(), 100, 10, instorage.getProviderid()));
            return "新货物输入成功";
        }

    }

    @Override
    public String outStorage(Integer productId, Integer number, Integer retailerId, Integer operatorId) {
        Warehouse warehouse = warehouseMapper.selectById(productId);
        Retailer retailer = retailerMapper.selectById(retailerId);
        if(warehouse==null) return "无此商品";
        else if(warehouse.getNownumber()<number) return "库存商品数量不足";
        else if(retailer==null) return "无此销售商";
        else {
            outstorageMapper.insert(new Outstorage(productId,warehouse.getProductname(),warehouse.getPrice(),new Date(),number,retailerId,operatorId));
            warehouse.setNownumber(warehouse.getNownumber()-number);
            warehouseMapper.updateById(warehouse);
            return "出货成功";
        }
    }

    @Override
    public User queryUserByName(String username) {
        List<User> users = userMapper.selectList(null);
        User user = users.stream().filter(x -> x.getUsername().equals(username)).findAny().orElse(new User(0, null, null, "", "", "", 0));
        if(user.getUsername().equals(""))
        return null;
        else return user;
    }

}
